package com.example.administrator.newfragmentation.activity.zhihu;

import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.example.administrator.newfragmentation.R;
import com.example.administrator.newfragmentation.event.TabSelectedEvent;
import com.example.administrator.newfragmentation.fragment.zhihu.first.ZhiHuFirstFragment;
import com.example.administrator.newfragmentation.fragment.zhihu.first.child.FirstHomeFragment;
import com.example.administrator.newfragmentation.fragment.zhihu.fourth.ZhiHuFourthFragment;
import com.example.administrator.newfragmentation.fragment.zhihu.fourth.child.MeFragment;
import com.example.administrator.newfragmentation.fragment.zhihu.second.ZhiHuSecondFragment;
import com.example.administrator.newfragmentation.fragment.zhihu.second.child.ViewPagerFragment;
import com.example.administrator.newfragmentation.fragment.zhihu.third.ZhiHuThirdFragment;
import com.example.administrator.newfragmentation.fragment.zhihu.third.child.ShopFragment;
import com.example.administrator.newfragmentation.fragmentMain.BaseZhiHuFragment;
import com.example.administrator.newfragmentation.view.BottomBar;
import com.example.administrator.newfragmentation.view.BottomBarTab;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.yokeyword.fragmentation.SupportActivity;
import me.yokeyword.fragmentation.SupportFragment;

public class ZhiHuMainActivity extends SupportActivity implements BaseZhiHuFragment.OnBackToFirstListener{

    @Bind(R.id.zhihu_fl_container)
    FrameLayout zhihuFlContainer;
    @Bind(R.id.zhihu_bottomBar)
    BottomBar zhihuBottomBar;

    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    public static final int FOURTH = 3;

    private SupportFragment[] mFragments = new SupportFragment[4];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhi_hu_main);
        ButterKnife.bind(this);
        if(savedInstanceState == null){
            mFragments[0] = ZhiHuFirstFragment.newInstance();
            mFragments[1] = ZhiHuSecondFragment.newInstance();
            mFragments[2] = ZhiHuThirdFragment.newInstance();
            mFragments[3] = ZhiHuFourthFragment.newInstance();

            loadMultipleRootFragment(R.id.zhihu_fl_container,FIRST,
                    mFragments[0],
                    mFragments[1],
                    mFragments[2],
                    mFragments[3]);
        }else{
            mFragments[0] = findFragment(ZhiHuFirstFragment.class);
            mFragments[1] = findFragment(ZhiHuSecondFragment.class);
            mFragments[2] = findFragment(ZhiHuThirdFragment.class);
            mFragments[3] = findFragment(ZhiHuFourthFragment.class);
        }

        initView();

    }

    private void initView() {
        zhihuBottomBar.addItem(new BottomBarTab(this, R.drawable.ic_home_white_24dp,""))
                .addItem(new BottomBarTab(this, R.drawable.ic_discover_white_24dp,""))
                .addItem(new BottomBarTab(this, R.drawable.ic_message_white_24dp,""))
                .addItem(new BottomBarTab(this, R.drawable.ic_account_circle_white_24dp,""));
        zhihuBottomBar.setOnTabSelectedListener(new BottomBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                showHideFragment(mFragments[position],mFragments[prePosition]);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {
                SupportFragment currentFragment = mFragments[position];
                int count = currentFragment.getChildFragmentManager().getBackStackEntryCount();
                Log.e("main_count",count+"...");
                // 如果不在该类别Fragment的主页,则回到主页;

                if (count > 1) {
                    if (currentFragment instanceof ZhiHuFirstFragment) {
                        currentFragment.popToChild(FirstHomeFragment.class, false);
                    } else if (currentFragment instanceof ZhiHuSecondFragment) {
                        currentFragment.popToChild(ViewPagerFragment.class, false);
                    } else if (currentFragment instanceof ZhiHuThirdFragment) {
                        currentFragment.popToChild(ShopFragment.class, false);
                    } else if (currentFragment instanceof ZhiHuFourthFragment) {
                        currentFragment.popToChild(MeFragment.class, false);
                    }
                    return;
                }

                if (count == 1) {
                    // 在FirstPagerFragment中接收, 因为是嵌套的孙子Fragment 所以用EventBus比较方便
                    // 主要为了交互: 重选tab 如果列表不在顶部则移动到顶部,如果已经在顶部,则刷新
                    EventBus.getDefault().post(new TabSelectedEvent(position));
                }
            }
        });
    }

    @Override
    public void onBackToFirstFragment() {
        zhihuBottomBar.setCurrentItem(0);
    }
}
